<?php
class Customer {
	private $customer_id;
	private $firstname;
	private $lastname;
	private $email;
	private $telephone;
	private $fax;
	private $newsletter;
	private $customer_group_id;
	private $address_id;
    private $master_slave;
    private $master_account_id;
    //add forum_user info
    private $forum_userId;
    private $forum_userName;
    private $forum_userType;
    private $forum_userIp;
    private $forum_regDate;
    private $forum_userBirthDay;
    private $forum_userLastVisitTime;
    private $forum_userLastPostTime;
    private $forum_userWarnings;
    private $forum_userLastWarning;
    private $forum_userBanId;
    private $forum_userTopics;
    private $forum_userPosts;
    private $forum_userRank;
    private $forum_userNewPm;
    private $forum_userAttachNotify;
    private $forum_userAttachBbcode;
    private $forum_userAttachSmiles;
    private $forum_userAttachSignature;
    private $forum_userNotifyPm;
    private $forum_userAllowPm;
    private $forum_userAllowEmail;
    private $forum_userAllowViewOnline;
    private $form_userAvatar;
    private $forum_userSignature;
    private $forum_userLocation;
    private $forum_userIcq;
    private $forum_userAim;
    private $forum_userYim;
    private $forum_userMsnm;
    private $forum_userJabber;
    private $forum_userOccupation;
    private $forum_userInterests;
    private $forum_userActkey;
    private $forum_points;

  	public function __construct($registry) {
		$this->config = $registry->get('config');
		$this->db = $registry->get('db');
		$this->request = $registry->get('request');
		$this->session = $registry->get('session');

		if (isset($this->session->data['customer_id'])) {//select forum_user info with LEFT JOIN
			$customer_query = $this->db->query("SELECT u.*, c.* FROM " . DB_PREFIX . "customer AS c LEFT JOIN " . DB_PREFIX . "community_users AS u ON (c.customer_id=u.customer_id) WHERE c.customer_id = '" . (int)$this->session->data['customer_id'] . "' AND c.status = '1'");

			if ($customer_query->num_rows) {
				$this->customer_id = $customer_query->row['customer_id'];
				$this->firstname = $customer_query->row['firstname'];
				$this->lastname = $customer_query->row['lastname'];
				$this->email = $customer_query->row['email'];
				$this->telephone = $customer_query->row['telephone'];
				$this->fax = $customer_query->row['fax'];
				$this->newsletter = $customer_query->row['newsletter'];
				$this->customer_group_id = $customer_query->row['customer_group_id'];
				$this->address_id = $customer_query->row['address_id'];
                $this->master_slave = $customer_query->row['master_slave'];
                $this->master_account_id = $customer_query->row['master_account_id'];
                //add forum_user info
                $this->forum_userId = $customer_query->row['user_id'];
                $this->forum_userName = $customer_query->row['username'];
                $this->forum_userType = $customer_query->row['user_type'];
                $this->forum_userIp = $customer_query->row['user_ip'];
                $this->forum_regDate = $customer_query->row['user_regdate'];
                $this->forum_userBirthDay = $customer_query->row['user_birthday'];
                $this->forum_userLastVisitTime = $customer_query->row['user_lastvisit'];
                $this->forum_userLastPostTime = $customer_query->row['user_lastpost_time'];
                $this->forum_userWarnings = $customer_query->row['user_warnings'];
                $this->forum_userLastWarning = $customer_query->row['user_last_warning'];
                $this->forum_userBanId = $customer_query->row['user_ban_id'];
                $this->forum_userTopics = $customer_query->row['user_topics'];
                $this->forum_userPosts = $customer_query->row['user_posts'];
                $this->forum_userRank = $customer_query->row['user_rank'];
                $this->forum_userNewPm = $customer_query->row['user_new_privmsg'];
                $this->forum_userAttachNotify = $customer_query->row['user_attach_notify'];
                $this->forum_userAttachBbcode = $customer_query->row['user_attach_bbcode'];
                $this->forum_userAttachSmiles = $customer_query->row['user_attach_smiles'];
                $this->forum_userAttachSignature = $customer_query->row['user_attach_sig'];
                $this->forum_userNotifyPm = $customer_query->row['user_notify_pm'];
                $this->forum_userAllowPm = $customer_query->row['user_allow_pm'];
                $this->forum_userAllowEmail = $customer_query->row['user_allow_email'];
                $this->forum_userAllowViewOnline = $customer_query->row['user_allow_viewonline'];
                $this->form_userAvatar = $customer_query->row['user_avatar'];
                $this->forum_userSignature = $customer_query->row['user_sig'];
                $this->forum_userLocation = $customer_query->row['user_from'];
                $this->forum_userIcq = $customer_query->row['user_icq'];
                $this->forum_userAim = $customer_query->row['user_aim'];
                $this->forum_userYim = $customer_query->row['user_yim'];
                $this->forum_userMsnm = $customer_query->row['user_msnm'];
                $this->forum_userJabber = $customer_query->row['user_jabber'];
                $this->forum_userOccupation = $customer_query->row['user_occ'];
                $this->forum_userInterests = $customer_query->row['user_interests'];
                $this->forum_userActkey = $customer_query->row['user_actkey'];
                $this->forum_points = $customer_query->row['forum_points'];

                $this->db->query("UPDATE " . DB_PREFIX . "customer SET cart = '" . $this->db->escape(isset($this->session->data['cart']) ? serialize($this->session->data['cart']) : '') . "', wishlist = '" . $this->db->escape(isset($this->session->data['wishlist']) ? serialize($this->session->data['wishlist']) : '') . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE customer_id = '" . (int)$this->session->data['customer_id'] . "'");

				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_ip WHERE customer_id = '" . (int)$this->session->data['customer_id'] . "' AND ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "'");

				if (!$query->num_rows) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "customer_ip SET customer_id = '" . (int)$this->session->data['customer_id'] . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', date_added = NOW()");
				}

                //associated accounts
                if(isset($this->request->cookie['cid']) && $this->request->cookie['cid'] == $customer_query->row['customer_id'] && empty($customer_query->row['master_slave'])){
                    $this->db->query("UPDATE " . DB_PREFIX . "customer SET master_slave='ma' WHERE customer_id='" . (int)$customer_query->row['customer_id'] . "'");
                }elseif(isset($this->request->cookie['cid']) && $this->request->cookie['cid'] != $customer_query->row['customer_id'] && empty($customer_query->row['master_slave'])){
                    $this->db->query("UPDATE " . DB_PREFIX . "customer SET master_slave='sl', master_account_id='" . (int)$this->request->cookie['cid'] . "' WHERE customer_id='" . (int)$customer_query->row['customer_id'] . "'");
                }

                //update forum user online status
                if(time()>=($this->session->data['session_last_visit_time']+($this->config->get("online_time_span")*60*5/6))){
                    $cur_time=time();

                    $this->db->query("UPDATE " . DB_PREFIX . "community_sessions SET session_last_visit_time='" . (int)$cur_time . "', session_viewonline='" . (int)$this->forum_userAllowViewOnline . "' WHERE session_user_id='" . (int)$this->forum_userId . "'");

                    $this->session->data['session_last_visit_time']=$cur_time;
                }
			} else {
				$this->logout();
			}
  		}else{
            if(isset($this->request->cookie['email']) && isset($this->request->cookie['password'])){//auto login
                $this->autoLogin($this->request->cookie['email'],$this->request->cookie['password']);
            }else{
                $this->forum_userType = 4;//forum guest
                $this->forum_userRank = 0;

                //record guest online status in a txt file
                $cur_time=time();
                $file='system/guest_online_list.txt';

                touch($file);//if file not exits, create it

                $file_record=file($file);
                $guest_online_record=array();

                foreach($file_record as $line){
                    $result=explode('|',$line);
                    $guest_online_record[$result[0]]=trim($result[1]);
                }

                if(!isset($this->request->cookie[$this->request->server['REMOTE_ADDR']])){
                    if(!array_key_exists($this->request->server['REMOTE_ADDR'],$guest_online_record)){
                        // ip record does not exist in the record
                        if(@$fp=fopen($file,'a')){
                            if(@flock($fp,LOCK_EX | LOCK_NB)){
                                $fline=$this->request->server['REMOTE_ADDR'] . '|' . $cur_time . "\n";
                                fwrite($fp,$fline);
                                flock($fp,LOCK_UN);
                            }
                            fclose($fp);
                        }
                    }elseif(array_key_exists($this->request->server['REMOTE_ADDR'],$guest_online_record) && ($guest_online_record[$this->request->server['REMOTE_ADDR']]+$this->config->get("online_time_span")*60<$cur_time)){
                        //or ip record exists in the record but active time has passed
                        $guest_online_record[$this->request->server['REMOTE_ADDR']]=$cur_time;

                        if(@$fp=fopen($file,'w')){
                            if(@flock($fp,LOCK_EX | LOCK_NB)){
                                rewind($fp);
                                $guest_online_str='';

                                foreach($guest_online_record as $guest_line_ip => $guest_line_time){
                                    $guest_online_str.=$guest_line_ip . '|' . $guest_line_time . "\n";
                                }

                                fwrite($fp,$guest_online_str);
                                flock($fp,LOCK_UN);
                            }

                            fclose($fp);
                        }
                    }

                    setcookie($this->request->server['REMOTE_ADDR'],$cur_time);
                }elseif($cur_time>=$this->request->cookie[$this->request->server['REMOTE_ADDR']]+$this->config->get("online_time_span")*60*5/6){
                    $guest_online_record[$this->request->server['REMOTE_ADDR']]=$cur_time;

                    if(@$fp=fopen($file,'w')){
                        if(@flock($fp,LOCK_EX | LOCK_NB)){
                            rewind($fp);
                            $guest_online_str='';

                            foreach($guest_online_record as $guest_line_ip => $guest_line_time){
                                $guest_online_str.=$guest_line_ip . '|' . $guest_line_time . "\n";
                            }

                            fwrite($fp,$guest_online_str);
                            flock($fp,LOCK_UN);
                        }

                        fclose($fp);
                    }

                    setcookie($this->request->server['REMOTE_ADDR'],$cur_time);
                }
            }
        }
	}
		
  	public function login($email, $password, $override = false, $autoLogin=false) {//and autologin && select forum_user info with LEFT JOIN
          if ($override) {
              $customer_query = $this->db->query("SELECT u.*, c.* FROM " . DB_PREFIX . "customer AS c LEFT JOIN " . DB_PREFIX . "community_users AS u ON(u.customer_id=c.customer_id) where LOWER(c.email) = '" . $this->db->escape(strtolower($email)) . "' AND c.status = '1'");
          } elseif (!$this->config->get('config_customer_approval')) {
              $customer_query = $this->db->query("SELECT u.*, c.* FROM " . DB_PREFIX . "customer AS c LEFT JOIN " . DB_PREFIX . "community_users AS u ON(u.customer_id=c.customer_id) where LOWER(c.email) = '" . $this->db->escape(strtolower($email)) . "' AND c.password = '" . $this->db->escape(md5($password)) . "' AND c.status = '1'");
          } else {
              $customer_query = $this->db->query("SELECT u.*, c.* FROM " . DB_PREFIX . "customer AS c LEFT JOIN " . DB_PREFIX . "community_users AS u ON(u.customer_id=c.customer_id) where LOWER(c.email) = '" . $this->db->escape(strtolower($email)) . "' AND c.password = '" . $this->db->escape(md5($password)) . "' AND c.status = '1' AND c.approved = '1'");
          }

		if ($customer_query->num_rows) {
			$this->session->data['customer_id'] = $customer_query->row['customer_id'];	
		    
			if ($customer_query->row['cart'] && is_string($customer_query->row['cart'])) {
				$cart = unserialize($customer_query->row['cart']);
				
				foreach ($cart as $key => $value) {
					if (!array_key_exists($key, $this->session->data['cart'])) {
						$this->session->data['cart'][$key] = $value;
					} else {
						$this->session->data['cart'][$key] += $value;
					}
				}			
			}

			if ($customer_query->row['wishlist'] && is_string($customer_query->row['wishlist'])) {
				if (!isset($this->session->data['wishlist'])) {
					$this->session->data['wishlist'] = array();
				}
								
				$wishlist = unserialize($customer_query->row['wishlist']);
			
				foreach ($wishlist as $product_id) {
					if (!in_array($product_id, $this->session->data['wishlist'])) {
						$this->session->data['wishlist'][] = $product_id;
					}
				}			
			}

			$this->customer_id = $customer_query->row['customer_id'];
			$this->firstname = $customer_query->row['firstname'];
			$this->lastname = $customer_query->row['lastname'];
			$this->email = $customer_query->row['email'];
			$this->telephone = $customer_query->row['telephone'];
			$this->fax = $customer_query->row['fax'];
			$this->newsletter = $customer_query->row['newsletter'];
			$this->customer_group_id = $customer_query->row['customer_group_id'];
			$this->address_id = $customer_query->row['address_id'];
            $this->master_slave = $customer_query->row['master_slave'];
            $this->master_account_id = $customer_query->row['master_account_id'];
            //add forum_user info
            $this->forum_userId = $customer_query->row['user_id'];
            $this->forum_userName = $customer_query->row['username'];
            $this->forum_userType = $customer_query->row['user_type'];
            $this->forum_userIp = $customer_query->row['user_ip'];
            $this->forum_regDate = $customer_query->row['user_regdate'];
            $this->forum_userBirthDay = $customer_query->row['user_birthday'];
            $this->forum_userLastVisitTime = $customer_query->row['user_lastvisit'];
            $this->forum_userLastPostTime = $customer_query->row['user_lastpost_time'];
            $this->forum_userWarnings = $customer_query->row['user_warnings'];
            $this->forum_userLastWarning = $customer_query->row['user_last_warning'];
            $this->forum_userBanId = $customer_query->row['user_ban_id'];
            $this->forum_userTopics = $customer_query->row['user_topics'];
            $this->forum_userPosts = $customer_query->row['user_posts'];
            $this->forum_userRank = $customer_query->row['user_rank'];
            $this->forum_userNewPm = $customer_query->row['user_new_privmsg'];
            $this->forum_userAttachNotify = $customer_query->row['user_attach_notify'];
            $this->forum_userAttachBbcode = $customer_query->row['user_attach_bbcode'];
            $this->forum_userAttachSmiles = $customer_query->row['user_attach_smiles'];
            $this->forum_userAttachSignature = $customer_query->row['user_attach_sig'];
            $this->forum_userNotifyPm = $customer_query->row['user_notify_pm'];
            $this->forum_userAllowPm = $customer_query->row['user_allow_pm'];
            $this->forum_userAllowEmail = $customer_query->row['user_allow_email'];
            $this->forum_userAllowViewOnline = $customer_query->row['user_allow_viewonline'];
            $this->form_userAvatar = $customer_query->row['user_avatar'];
            $this->forum_userSignature = $customer_query->row['user_sig'];
            $this->forum_userLocation = $customer_query->row['user_from'];
            $this->forum_userIcq = $customer_query->row['user_icq'];
            $this->forum_userAim = $customer_query->row['user_aim'];
            $this->forum_userYim = $customer_query->row['user_yim'];
            $this->forum_userMsnm = $customer_query->row['user_msnm'];
            $this->forum_userJabber = $customer_query->row['user_jabber'];
            $this->forum_userOccupation = $customer_query->row['user_occ'];
            $this->forum_userInterests = $customer_query->row['user_interests'];
            $this->forum_userActkey = $customer_query->row['user_actkey'];
            $this->forum_points = $customer_query->row['forum_points'];

            //also update user_ip, user_lastvisit in forum_user table
			$this->db->query("UPDATE " . DB_PREFIX . "customer AS c, " . DB_PREFIX . "community_users AS u SET c.ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', u.user_ip='" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', u.user_lastvisit='" . time() . "' WHERE c.customer_id = '" . (int)$this->customer_id . "' AND u.customer_id = '" . (int)$this->customer_id . "'");

            //delete guest online record from file
            $file='system/guest_online_list.txt';

            $file_record=file($file);
            $guest_online_record=array();

            foreach($file_record as $line){
                $result=explode('|',$line);
                if($result[0]!=$this->request->server['REMOTE_ADDR']){
                    $guest_online_record[$result[0]]=trim($result[1]);
                }
            }

            if(@$fp=fopen($file,'w')){
                if(@flock($fp,LOCK_EX | LOCK_NB)){
                    rewind($fp);
                    $guest_online_str='';

                    foreach($guest_online_record as $guest_line_ip => $guest_line_time){
                        $guest_online_str.=$guest_line_ip . '|' . $guest_line_time . "\n";
                    }

                    fwrite($fp,$guest_online_str);
                    flock($fp,LOCK_UN);
                }

                fclose($fp);
            }

            //update or insert into the community_sessions table
            $user_session_query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_sessions WHERE session_user_id='" . (int)$this->forum_userId . "' LIMIT 0,1");

            $cur_time=time();

            if($user_session_query->num_rows){
                $this->db->query("UPDATE " . DB_PREFIX . "community_sessions SET session_last_visit_time='" . (int)$cur_time . "', session_viewonline='" . (int)$this->forum_userAllowViewOnline . "' WHERE session_user_id='" . (int)$this->forum_userId . "'");
            }else{
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_sessions SET session_user_id='" . (int)$this->forum_userId . "', session_last_visit_time='" . (int)$cur_time . "', session_viewonline='" . (int)$this->forum_userAllowViewOnline . "'");
            }

            $this->session->data['session_last_visit_time']=$cur_time;

           //set cookie for auto login
           if($autoLogin){
               setcookie('email',$email,2147299200);
               setcookie('password',md5($password),2147299200);
           }

	  		return true;
    	} else {
      		return false;
    	}
  	}

    private function autoLogin($email, $password) {//new function for auto login
        if (!$this->config->get('config_customer_approval')) {
            $customer_query = $this->db->query("SELECT u.*, c.* FROM " . DB_PREFIX . "customer AS c LEFT JOIN " . DB_PREFIX . "community_users AS u ON(c.customer_id=u.customer_id) where LOWER(c.email) = '" . $this->db->escape(strtolower($email)) . "' AND c.password = '" . $this->db->escape($password) . "' AND c.status = '1'");
        } else {
            $customer_query = $this->db->query("SELECT u.*, c.* FROM " . DB_PREFIX . "customer AS c LEFT JOIN " . DB_PREFIX . "community_users AS u ON(c.customer_id=u.customer_id) where LOWER(c.email) = '" . $this->db->escape(strtolower($email)) . "' AND c.password = '" . $this->db->escape($password) . "' AND c.status = '1' AND c.approved = '1'");
        }

        if ($customer_query->num_rows) {
            $this->session->data['customer_id'] = $customer_query->row['customer_id'];

            if ($customer_query->row['cart'] && is_string($customer_query->row['cart'])) {
                $cart = unserialize($customer_query->row['cart']);

                if(!isset($this->session->data['cart'])){
                    $this->session->data['cart']=array();
                }

                foreach ($cart as $key => $value) {
                    if (!array_key_exists($key, $this->session->data['cart'])) {
                        $this->session->data['cart'][$key] = $value;
                    } else {
                        $this->session->data['cart'][$key] += $value;
                    }
                }
            }

            if ($customer_query->row['wishlist'] && is_string($customer_query->row['wishlist'])) {
                if (!isset($this->session->data['wishlist'])) {
                    $this->session->data['wishlist'] = array();
                }

                $wishlist = unserialize($customer_query->row['wishlist']);

                foreach ($wishlist as $product_id) {
                    if (!in_array($product_id, $this->session->data['wishlist'])) {
                        $this->session->data['wishlist'][] = $product_id;
                    }
                }
            }

            $this->customer_id = $customer_query->row['customer_id'];
            $this->firstname = $customer_query->row['firstname'];
            $this->lastname = $customer_query->row['lastname'];
            $this->email = $customer_query->row['email'];
            $this->telephone = $customer_query->row['telephone'];
            $this->fax = $customer_query->row['fax'];
            $this->newsletter = $customer_query->row['newsletter'];
            $this->customer_group_id = $customer_query->row['customer_group_id'];
            $this->address_id = $customer_query->row['address_id'];
            $this->master_slave = $customer_query->row['master_slave'];
            $this->master_account_id = $customer_query->row['master_account_id'];
            //add forum_user info
            $this->forum_userId = $customer_query->row['user_id'];
            $this->forum_userName = $customer_query->row['username'];
            $this->forum_userType = $customer_query->row['user_type'];
            $this->forum_userIp = $customer_query->row['user_ip'];
            $this->forum_regDate = $customer_query->row['user_regdate'];
            $this->forum_userBirthDay = $customer_query->row['user_birthday'];
            $this->forum_userLastVisitTime = $customer_query->row['user_lastvisit'];
            $this->forum_userLastPostTime = $customer_query->row['user_lastpost_time'];
            $this->forum_userWarnings = $customer_query->row['user_warnings'];
            $this->forum_userLastWarning = $customer_query->row['user_last_warning'];
            $this->forum_userBanId = $customer_query->row['user_ban_id'];
            $this->forum_userTopics = $customer_query->row['user_topics'];
            $this->forum_userPosts = $customer_query->row['user_posts'];
            $this->forum_userRank = $customer_query->row['user_rank'];
            $this->forum_userNewPm = $customer_query->row['user_new_privmsg'];
            $this->forum_userAttachNotify = $customer_query->row['user_attach_notify'];
            $this->forum_userAttachBbcode = $customer_query->row['user_attach_bbcode'];
            $this->forum_userAttachSmiles = $customer_query->row['user_attach_smiles'];
            $this->forum_userAttachSignature = $customer_query->row['user_attach_sig'];
            $this->forum_userNotifyPm = $customer_query->row['user_notify_pm'];
            $this->forum_userAllowPm = $customer_query->row['user_allow_pm'];
            $this->forum_userAllowEmail = $customer_query->row['user_allow_email'];
            $this->forum_userAllowViewOnline = $customer_query->row['user_allow_viewonline'];
            $this->form_userAvatar = $customer_query->row['user_avatar'];
            $this->forum_userSignature = $customer_query->row['user_sig'];
            $this->forum_userLocation = $customer_query->row['user_from'];
            $this->forum_userIcq = $customer_query->row['user_icq'];
            $this->forum_userAim = $customer_query->row['user_aim'];
            $this->forum_userYim = $customer_query->row['user_yim'];
            $this->forum_userMsnm = $customer_query->row['user_msnm'];
            $this->forum_userJabber = $customer_query->row['user_jabber'];
            $this->forum_userOccupation = $customer_query->row['user_occ'];
            $this->forum_userInterests = $customer_query->row['user_interests'];
            $this->forum_userActkey = $customer_query->row['user_actkey'];
            $this->forum_points = $customer_query->row['forum_points'];

            //also update user_ip, user_lastvisit in forum_user table
            $this->db->query("UPDATE " . DB_PREFIX . "customer AS c, " . DB_PREFIX . "community_users AS u SET c.ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', u.user_ip='" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', u.user_lastvisit='" . time() . "' WHERE c.customer_id = '" . (int)$this->customer_id . "' AND u.customer_id = '" . (int)$this->customer_id . "'");

            //delete guest online record from file
            $file='system/guest_online_list.txt';

            $file_record=file($file);
            $guest_online_record=array();

            foreach($file_record as $line){
                $result=explode('|',$line);
                if($result[0]!=$this->request->server['REMOTE_ADDR']){
                    $guest_online_record[$result[0]]=trim($result[1]);
                }
            }

            if(@$fp=fopen($file,'w')){
                if(@flock($fp,LOCK_EX | LOCK_NB)){
                    rewind($fp);
                    $guest_online_str='';

                    foreach($guest_online_record as $guest_line_ip => $guest_line_time){
                        $guest_online_str.=$guest_line_ip . '|' . $guest_line_time . "\n";
                    }

                    fwrite($fp,$guest_online_str);
                    flock($fp,LOCK_UN);
                }

                fclose($fp);
            }

            //update or insert into the community_sessions table
            $user_session_query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_sessions WHERE session_user_id='" . (int)$this->forum_userId . "' LIMIT 0,1");

            $cur_time=time();

            if($user_session_query->num_rows){
                $this->db->query("UPDATE " . DB_PREFIX . "community_sessions SET session_last_visit_time='" . (int)$cur_time . "', session_viewonline='" . (int)$this->forum_userAllowViewOnline . "' WHERE session_user_id='" . (int)$this->forum_userId . "'");
            }else{
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_sessions SET session_user_id='" . (int)$this->forum_userId . "', session_last_visit_time='" . (int)$cur_time . "', session_viewonline='" . (int)$this->forum_userAllowViewOnline . "'");
            }

            $this->session->data['session_last_visit_time']=$cur_time;

            return true;
        } else {
            return false;
        }
    }

	public function logout() {
		$this->db->query("UPDATE " . DB_PREFIX . "customer SET cart = '" . $this->db->escape(isset($this->session->data['cart']) ? serialize($this->session->data['cart']) : '') . "', wishlist = '" . $this->db->escape(isset($this->session->data['wishlist']) ? serialize($this->session->data['wishlist']) : '') . "' WHERE customer_id = '" . (int)$this->customer_id . "'");

        $this->db->query("DELETE FROM " . DB_PREFIX . "community_sessions WHERE session_user_id='" . (int)$this->forum_userId . "'");

		unset($this->session->data['customer_id']);
        unset($this->session->data['session_last_visit_time']);

        //delete auto login cookie details
        if(isset($this->request->cookie['email']) && isset($this->request->cookie['password'])){
            setcookie('email','',time()-3600);
            setcookie('password','',time()-3600);
        }

		$this->customer_id = '';
		$this->firstname = '';
		$this->lastname = '';
		$this->email = '';
		$this->telephone = '';
		$this->fax = '';
		$this->newsletter = '';
		$this->customer_group_id = '';
		$this->address_id = '';
        $this->master_slave = '';
        $this->master_account_id = '';
        //add forum_user info
        $this->forum_userId = '';
        $this->forum_userName = '';
        $this->forum_userType = 4;//forum guest
        $this->forum_userIp = '';
        $this->forum_regDate = '';
        $this->forum_userBirthDay = '';
        $this->forum_userLastVisitTime = '';
        $this->forum_userLastPostTime = '';
        $this->forum_userWarnings = '';
        $this->forum_userLastWarning = '';
        $this->forum_userBanId = '';
        $this->forum_userTopics = '';
        $this->forum_userPosts = '';
        $this->forum_userRank = 0;//forum guest
        $this->forum_userNewPm = '';
        $this->forum_userAttachNotify = '';
        $this->forum_userAttachBbcode = '';
        $this->forum_userAttachSmiles = '';
        $this->forum_userAttachSignature = '';
        $this->forum_userNotifyPm = '';
        $this->forum_userAllowPm = '';
        $this->forum_userAllowEmail = '';
        $this->forum_userAllowViewOnline = '';
        $this->form_userAvatar = '';
        $this->forum_userSignature = '';
        $this->forum_userLocation = '';
        $this->forum_userIcq = '';
        $this->forum_userAim = '';
        $this->forum_userYim = '';
        $this->forum_userMsnm = '';
        $this->forum_userJabber = '';
        $this->forum_userOccupation = '';
        $this->forum_userInterests = '';
        $this->forum_userActkey = '';
        $this->forum_points = '';
  	}
  
  	public function isLogged() {
    	return $this->customer_id;
  	}

  	public function getId() {
    	return $this->customer_id;
  	}
      
  	public function getFirstName() {
		return $this->firstname;
  	}
  
  	public function getLastName() {
		return $this->lastname;
  	}
  
  	public function getEmail() {
		return $this->email;
  	}
  
  	public function getTelephone() {
		return $this->telephone;
  	}
  
  	public function getFax() {
		return $this->fax;
  	}
	
  	public function getNewsletter() {
		return $this->newsletter;	
  	}

  	public function getCustomerGroupId() {
		return $this->customer_group_id;	
  	}
	
  	public function getAddressId() {
		return $this->address_id;	
  	}

    // get forum user data;
    public function getMasterSlave(){
        return $this->master_slave;
    }

    public function getMasterAccountId(){
        return $this->master_account_id;
    }

    public function getForumUserId(){
        return $this->forum_userId;
    }

    public function getForumUserName(){
        return $this->forum_userName;
    }

    public function getForumUserType(){
        return $this->forum_userType;
    }

    public function getForumUserIp(){
        return $this->forum_userIp;
    }

    public function getForumRegDate(){
        return $this->forum_regDate;
    }

    public function getForumUserBirthDay(){
        return $this->forum_userBirthDay;
    }

    public function getForumUserLastVisitTime(){
        return $this->forum_userLastVisitTime;
    }

    public function getForumUserLastPostTime(){
        return $this->forum_userLastPostTime;
    }

    public function getForumUserWarnings(){
        return $this->forum_userWarnings;
    }

    public function getForumUserBanId(){
        return $this->forum_userBanId;
    }

    public  function  getForumUserTopics(){
        return $this->forum_userTopics;
    }

    public  function  getForumUserPosts(){
        return $this->forum_userPosts;
    }

    public function getForumUserRank(){
        return $this->forum_userRank;
    }

    public  function  getForumUserNewPm(){
        return $this->forum_userNewPm;
    }

    public function getForumUserAttachNotify(){
        return $this->forum_userAttachNotify;
    }

    public function getForumUserAttachBbcode(){
        return $this->forum_userAttachBbcode;
    }

    public function getForumUserAttachSmiles(){
        return $this->forum_userAttachSmiles;
    }

    public function getForumUserAttachSignature(){
        return $this->forum_userAttachSignature;
    }

    public function getForumUserNotifyPm(){
        return $this->forum_userNotifyPm;
    }

    public function getForumUserAllowPm(){
        return $this->forum_userAllowPm;
    }

    public function getForumUserAllowEmail(){
        return $this->forum_userAllowEmail;
    }

    public function getForumUserAllowViewOnline(){
        return $this->forum_userAllowViewOnline;
    }

    public  function getForumUserAvatar(){
        return $this->form_userAvatar;
    }

    public  function getForumUserSignature(){
        return $this->forum_userSignature;
    }

    public function getForumUserLocation(){
        return $this->forum_userLocation;
    }

    public function getForumUserIcq(){
        return $this->forum_userIcq;
    }

    public function getForumUserAim(){
        return $this->forum_userAim;
    }

    public function getForumUserYim(){
        return $this->forum_userYim;
    }

    public function getForumUserMsnm(){
        return $this->forum_userMsnm;
    }

    public function getForumUserJabber(){
        return $this->forum_userJabber;
    }

    public  function  getForumUserOccupation(){
        return $this->forum_userOccupation;
    }

    public  function  getForumUserInterests(){
        return $this->forum_userInterests;
    }

    public  function  getForumUserActkey(){
        return $this->forum_userActkey;
    }
    public  function  getForumPoints(){
        return $this->forum_points;
    }

  	public function getBalance() {
		$query = $this->db->query("SELECT SUM(amount) AS total FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$this->customer_id . "'");
	
		return $query->row['total'];
  	}	
		
  	public function getRewardPoints() {
		$query = $this->db->query("SELECT SUM(points) AS total FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$this->customer_id . "'");
	
		return $query->row['total'];	
  	}	
}
?>